package cn.edu.mju.dilidili.modules.user.serviceImpl;

import cn.edu.mju.dilidili.common.utils.ConnectUtil;
import cn.edu.mju.dilidili.common.utils.FinalVariable;
import cn.edu.mju.dilidili.common.utils.Result;
import cn.edu.mju.dilidili.common.utils.StateCode;
import cn.edu.mju.dilidili.modules.user.dto.UserResponseDto;
import cn.edu.mju.dilidili.modules.user.entity.User;
import cn.edu.mju.dilidili.modules.user.mapper.UserMapper;
import cn.edu.mju.dilidili.modules.user.service.UserService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author l
 * @since 2022-09-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result<UserResponseDto> login(String username, String password) {
        User user = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username)
                .eq(User::getPassword, password)
        );
        UserResponseDto userDto = BeanUtil.copyProperties(user, UserResponseDto.class);
        HashMap<String, Object> map = new HashMap<>();
        BeanUtil.copyProperties(user, map);
        map.put(FinalVariable.AUTHORIZATION, new String[]{"admin"});
        ConnectUtil.putJWT(map);
        return new Result<>(StateCode.LOGIN_ERROR, userDto);
    }
}
